package com.laan.bmagame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class LaanGameSV extends SurfaceView {
	
	private SurfaceHolder holder;
	private LaanGameThread lgt;
	private float xtouch;
	
	//classes auxiliars
	private Nuvols nuvols;
	private Peces peces;
	private Base base;
	//control thread
	private boolean surfacecreated;
	private boolean threadrunning;
	//fons
	Paint fons;
	
	//moviment
	private boolean zoom = false;
	private float oldDist;
	private float[] lastx;
	private int[] pointerId;
	
	public LaanGameSV(Context context) {
		super(context);
		init();
	}

	public LaanGameSV(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public LaanGameSV(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {
		surfacecreated = false;
		threadrunning = false;
		holder = getHolder();
		holder.addCallback( new Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				stopthread();
				surfacecreated = false;
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				holder.setFormat(PixelFormat.RGBA_8888);
				
				fons = new Paint(); 
			    Shader shader = new LinearGradient(0,0,GV.Screen.metrics.widthPixels,GV.Screen.metrics.heightPixels,new int[]{0xFFA4D8FF,0xFF0031D2},null,Shader.TileMode.CLAMP);
			    fons.setShader(shader);  
				
				nuvols = new Nuvols(getResources());
				peces = new Peces(getResources());
				base = new Base();
				pointerId = new int[2];
				lastx = new float[2];
				
				surfacecreated = true;
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
				startthread();
			}
		});  
	}
	
	@Override
	protected void onDraw(Canvas canvas) { 
		canvas.drawRect(0,0,getWidth(),getHeight(), fons);
		nuvols.actualitza();
		peces.actualitza();
//		base.actualitzaPos(xtouch);
		
		nuvols.draw(canvas);
		base.draw(canvas);
		peces.draw(canvas);
		
		peces.colisions(base.x, base.y, base.width, base.height);
	}
	
	public void startthread() {
		if(!surfacecreated) return;
		if(threadrunning) return;
		lgt = new LaanGameThread(this);
		lgt.setRunning(true);
		lgt.start();
		threadrunning = true;
	}
	
	public void stopthread() {
		if(!surfacecreated) return;
		if(!threadrunning) return;
		boolean retry = true;
		while (retry) {
			try {
				lgt.setRunning(false);
				lgt.join();
				threadrunning = false;
				retry = false;
				
			} catch (InterruptedException e) {}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
//		Log.i("touch", String.valueOf(event.getX()));
//		if(event.getX() > base.x) xtouch = 10;
//		else xtouch = -10;

		if(event.getActionMasked() == MotionEvent.ACTION_DOWN) {
			int actInd = event.getActionIndex();
//			Log.i("action_down", String.valueOf(event.getActionIndex()));
//			Log.i("action_down", String.valueOf(event.getX()));
			Log.i("action_down", String.valueOf(event.getPointerId(actInd)));
			
			pointerId[actInd] = event.getPointerId(actInd);
			lastx[actInd] = event.getX(actInd);
		}
		if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
			oldDist = spacing(event);
			if(oldDist > 10f) {
				zoom = true;
			}
			int actInd = event.getActionIndex();
//			Log.i("action_pointer_down", String.valueOf(event.getPointerId(actInd)));
//			
//			pointerId[actInd] = event.getPointerId(actInd);
			lastx[actInd] = event.getX(actInd);
			
		}
		if(event.getActionMasked() == MotionEvent.ACTION_UP) {
			int actInd = event.getActionIndex();
			pointerId[actInd] = -1;
			return false;
		}
		if(event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
			int actInd = event.getActionIndex();
			Log.i("action_pointer_up", String.valueOf(event.getPointerId(actInd)));
			
			pointerId[actInd] = -1;
//			lastx[actInd] = event.getX();
			zoom = false;
			base.saveWidth();
		}
		if(event.getActionMasked() == MotionEvent.ACTION_MOVE) {
			int actInd = event.getActionIndex();
//			Log.i("action_move", String.valueOf(event.getPointerId(actInd)));
			
//			if(pointerId[actInd] == event.getPointerId(actInd)) {
			if(zoom) {
				float newDist = spacing(event);
				if(newDist > 10f) {
					float scale = newDist/oldDist;
					base.setWidth(scale);
					lastx[actInd] = event.getX(actInd);
				}
			}
			else {
				xtouch = 2f * (event.getX(actInd) - lastx[actInd]);
				base.actualitzaPos(xtouch);
				lastx[actInd] = event.getX(actInd);
			}
//			if(pointerId[0] == event.getPointerId(actInd)) {
//				
//			}
//			xtouch = 2f * (event.getX() - lastx);
//			base.actualitzaPos(xtouch);
//			lastx = event.getX();
		}
		return true;
	}
	
	private float spacing(MotionEvent event) {
	   float x = event.getX(0) - event.getX(1);
	   float y = event.getY(0) - event.getY(1);
	   return FloatMath.sqrt(x * x + y * y);
	}
	
	

	
}
